package com.liang.manage.auth.facade.server.iml;

import com.alibaba.fastjson.JSON;
import com.liang.manage.auth.facade.server.UrlAccessRightService;
import com.liang.nansheng.common.auth.PermissionsSsoDTO;
import com.liang.nansheng.common.auth.RoleSsoDTO;
import com.liang.nansheng.common.auth.UserSsoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 马亮南生
 * @date 2021-04-20 22:04
 */
@Slf4j
@Service
@Component
public class UrlAccessRightServiceImpl implements UrlAccessRightService {
    /**
     * 检查Url权限
     *
     * @param currentUser
     * @param url
     * @return
     */
    @Override
    public Boolean checkUrlAccess(UserSsoDTO currentUser, String url) {
        // 检查userSsoDTO里的rights是否含有当前uri的权限
        List<RoleSsoDTO> roles = currentUser.getRoles();
        if (CollectionUtils.isNotEmpty(roles)) {
            for (RoleSsoDTO roleSsoDTO : roles) {
                System.out.println("0:" + JSON.toJSONString(roleSsoDTO));
                List<PermissionsSsoDTO> permissions = roleSsoDTO.getPermissions();
                if (CollectionUtils.isNotEmpty(permissions)) {
                    System.out.println("1:" + JSON.toJSONString(permissions));
                    for (PermissionsSsoDTO permissionsSsoDTO : permissions) {
                        System.out.println("2:" + JSON.toJSONString(permissionsSsoDTO));
                        if (permissionsSsoDTO.getApi().equals(url)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

}
